//
//  LRXBuyThingCell.m
//  路人侠
//
//  Created by zhangxulong on 3/12/16.
//  Copyright © 2016 zhangxulong. All rights reserved.
//

#import "LRXBuyThingCell.h"
#import "AppDelegate.h"
#import "LRXBuyThingController.h"
#import "LRXLMeController.h"
@interface LRXBuyThingCell()
@property (nonatomic,strong) UIImageView * thingIcon;
@property (nonatomic,strong) UILabel * thingName;
@property (nonatomic,strong) UILabel * deatailText;
@property (nonatomic,strong) UILabel * thingCount;
@property (nonatomic,strong) UIButton * jiaBtn;
@property (nonatomic,strong) UIButton * jianBtn;
@property (nonatomic,strong) UILabel * thingMoney;
@property (nonatomic,strong) UIView * pointView;
@property (nonatomic,strong) NSMutableArray * selectPointArray;

@end

//全局静态变量 一个类就维护一个变量
static NSMutableArray * buyThingArray;
static NSMutableArray * selectedThingArray;


//static NSMutableArray * selectPointArray;

@implementation LRXBuyThingCell
+(void)initialize
{
    
    selectedThingArray = [NSMutableArray arrayWithCapacity:3];
    buyThingArray = [NSMutableArray arrayWithCapacity:3];

    
    
}

-(NSMutableArray *)selectPointArray
{
    if (_selectPointArray == nil) {
        _selectPointArray = [NSMutableArray array];
    }
    return _selectPointArray;
}





+(NSMutableArray *)getSelectedArray{
    
    return selectedThingArray;
    
}
+(void)setSelectedArray:(NSMutableArray *)selectedThings{

    selectedThingArray = selectedThings;
    
}





+ (void)setConfirmbuyArray:(NSMutableArray *)buyThings
{
  
    
     [selectedThingArray removeAllObjects];

    [selectedThingArray addObjectsFromArray:buyThings];

}


+(NSMutableArray *)getBuyThing
{
    return buyThingArray;
}
+ (void)setBuyThing:(NSMutableArray *)buyThing
{
    
    buyThingArray = buyThing;
    
}




- (void)layoutBuyThingCellSubView
{
    
    //按钮的动态布局
    [self layoutBtn];
    
    //共享变量
    
    UIImageView * thingIcon = [[UIImageView alloc]init];
    UILabel * thingName = [[UILabel alloc]init];
    UILabel * thingMoney = [[UILabel alloc]init];
    UILabel * deatailText = [[UILabel alloc]init];
    self.thingIcon = thingIcon;
    self.thingName = thingName;
    self.thingMoney = thingMoney;
    self.deatailText = deatailText;
    
    
   
    
    [self setBuyThingCellModel];
    
    thingIcon.frame = CGRectMake(27 * wScale, 37 * hScale, 110 * hScale, 110 * hScale);
    thingName.frame = CGRectMake(158 * wScale, 44 * hScale, 0, 0);
    thingName.font = [UIFont systemFontOfSize:28 * hScale];
    [thingName sizeToFit];
    
    thingMoney.textAlignment = NSTextAlignmentRight;
    thingMoney.font = [UIFont systemFontOfSize:28 * hScale];
    thingMoney.textColor = [UIColor colorWithRed:255 green:156 blue:0];
    thingMoney.frame = CGRectMake(CGRectGetMaxX(self.jiaBtn.frame) - 150 * wScale, 40 * hScale, 120 * wScale, 28 * hScale);
    
    CGFloat deaY = CGRectGetMaxY(thingName.frame) + 24 * hScale;
    deatailText.frame = CGRectMake(thingName.frame.origin.x, deaY, 150 * wScale, 24 * hScale);
    deatailText.font = [UIFont systemFontOfSize:24 * hScale];
    deatailText.textColor = mainGrayColor;
    
    [self addSubview:thingName];
    [self addSubview:thingIcon];
    [self addSubview:deatailText];
    [self addSubview:thingMoney];
    
}
/**
 *  请求到新的数据显示到cell中，通过全局静态变量buyThingArray 获取以前的值
 */
- (void)setBuyThingCellModel
{
    
    //照片的设置
    NSString * imageStr;
    if (self.thingInfo.thingIcon   && self.thingInfo.thingIcon.length > 0) {
        imageStr = [routeImageUrl stringByAppendingString:self.thingInfo.thingIcon];
    }
    //转UTF8 编码
    NSURL * imageUrl = [NSURL URLWithString: [imageStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]]];
    [self.thingIcon sd_setImageWithURL:imageUrl placeholderImage:[UIImage imageNamed:@"de"]];
    
    NSString * selectCount;
    for (LRXThing * thing in [LRXBuyThingCell getSelectedArray]) {
        
        if ([thing.thingId isEqualToString:self.thingInfo.thingId]) {
            
            selectCount = thing.thingCount;
            
        }
    }
    
    if (!selectCount) {
        self.jianBtn.hidden = YES;
        self.thingCount.hidden = YES;
    }
    else
    {
        self.jianBtn.hidden = NO;
        self.thingCount.hidden = NO;
    }
    self.thingName.text = self.thingInfo.thingName;
    self.thingCount.text = selectCount;
    self.deatailText.text = self.thingInfo.detailText;
    self.thingCount.textAlignment = NSTextAlignmentCenter;
    self.thingMoney.text = [self.thingInfo.thingMoney stringByAppendingString:@"元"];
    
}

- (void)layoutBtn
{
    CGFloat jiaBtnY =  98 * hScale;
    CGFloat jianBtnX = 317 * wScale;
    CGFloat interW = 0;
    
    UIButton * jianBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    UILabel * thingCount = [[UILabel alloc]init];
    UIButton * jiaBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    
    jianBtn.frame = CGRectMake(jianBtnX, jiaBtnY, 60 * hScale, 60 * hScale);
    jianBtn.contentEdgeInsets = UIEdgeInsetsMake(10 * hScale, 10 * hScale, 10 * hScale, 10 * hScale );
    
    CGFloat thingCountX = CGRectGetMaxX(jianBtn.frame) + interW;
    thingCount.frame = CGRectMake(thingCountX, jiaBtnY, 44 * wScale, 60 * hScale);
    thingCount.font = [UIFont systemFontOfSize:28 * hScale];
    thingCount.textAlignment = NSTextAlignmentCenter;
    
    CGFloat jiaBtnX = CGRectGetMaxX(thingCount.frame) + interW;
    jiaBtn.frame = CGRectMake(jiaBtnX, jiaBtnY, 60 * hScale, 60 *hScale);
    jiaBtn.contentEdgeInsets = UIEdgeInsetsMake(10 * hScale, 10 * hScale, 10 * hScale, 10 * hScale);
    if ([thingCount.text integerValue] <=0) {
        jianBtn.hidden = YES;
        thingCount.hidden = YES;
    }
    
    [jiaBtn setImage:[UIImage imageNamed:@"jiahao"] forState:UIControlStateNormal];
    [jianBtn setImage:[UIImage imageNamed:@"jianhao"] forState:UIControlStateNormal];
    
    jiaBtn.tag = 1;
    jianBtn.tag = 0;
    
    [jiaBtn addTarget:self action:@selector(thingCountBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [jianBtn addTarget:self action:@selector(thingCountBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    self.jianBtn = jianBtn;
    self.jiaBtn = jiaBtn;
    self.thingCount = thingCount;
    
    [self addSubview:self.jianBtn];
    [self addSubview:self.jiaBtn];
    [self addSubview:self.thingCount];
    
}

/**
 *  点击加号和减号的 物品的数量也跟着变化
 *
 *  @param btn btn.tag 为1 的时候 点击的加号
 *             btn.tag 为0 的时候 点击了减号
 */
- (void)thingCountBtnClick:(UIButton *)btn
{
    NSString * thingCount;
    //点击了加号
    if (btn.tag == 1) {
        thingCount = [NSString stringWithFormat:@"%d",[self.thingCount.text integerValue] + 1];
        
        NSInteger flag = 0;
        
        for (LRXThing * thing in [LRXBuyThingCell getSelectedArray]) {
            //找到了thing 进行相应的操作
            if ([thing.thingId isEqualToString:self.thingInfo.thingId]) {
                
                
                if ([[LRXBuyThingCell getBuyThing] containsObject:thing]) {
                    thing.reChonoseAddCount ++;
                }
                
                flag = 1;
                thing.thingCount = thingCount;
                break;
            }
        }
        if(flag == 0) {
            
            
            self.thingInfo.thingCount = @"1";
            [[LRXBuyThingCell getSelectedArray] addObject:self.thingInfo];
            
        }
        
        
        CGPoint point = self.thingCount.center;
        point = [self convertPoint:point toView:[UIApplication sharedApplication].keyWindow];
        
        UIView * pointView = [[UIView alloc]init];
        pointView.frame = CGRectMake(0, 0, 20 * hScale, 20 * hScale);
        pointView.backgroundColor = [UIColor redColor];
        pointView.layer.cornerRadius = pointView.frame.size.width * 0.5;
        pointView.layer.masksToBounds = YES;
        [[UIApplication sharedApplication].keyWindow addSubview:pointView];
        [self.selectPointArray addObject:pointView];
        
        AppDelegate * appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
        CAKeyframeAnimation *bounceAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
        CGMutablePathRef thePath = CGPathCreateMutable();
        CGPathMoveToPoint(thePath, NULL, point.x,point.y);
        CGPathAddQuadCurveToPoint(thePath, NULL, point.x - 200 * wScale, point.y - 100 * hScale, appDelegate.pointerCenter.x, appDelegate.pointerCenter.y);   //startPoint是起点，endPoint是终点，150，30是x,y轴的控制点，自行调整数值可以出现理想弧度效果
        bounceAnimation.path = thePath;

        bounceAnimation.removedOnCompletion=YES;
        bounceAnimation.fillMode=kCAFillModeForwards;
        
        bounceAnimation.duration = 0.4;
        bounceAnimation.delegate = self;
        pointView.center = appDelegate.pointerCenter;
        [pointView.layer addAnimation:bounceAnimation forKey:@"move"];
        
        

    }//点击了减号
    else{
        
        NSInteger pp = [self.thingCount.text integerValue] - 1;
        thingCount = [NSString stringWithFormat:@"%ld",pp];
        NSInteger flag = 0;
        for (LRXThing * thing in [LRXBuyThingCell getSelectedArray]) {
                //找到了thing 进行相应的操作
            if ([thing.thingId isEqualToString:self.thingInfo.thingId]) {
                
                if ([[LRXBuyThingCell getBuyThing] containsObject:thing]) {
                        thing.reChonoseSubCount ++;
                }
                
                flag = 1;
                
                NSInteger p = [thing.thingCount integerValue]-1;
                thing.thingCount = [NSString stringWithFormat:@"%ld",(long)p];
                
                
                
                if (p <= 0) {
                    
                    thing.thingCount = @"0";
                    self.thingCount.text = @"0";
                    
                    
                    if ([[LRXBuyThingCell getBuyThing] containsObject:thing]) {
                        
                        [[LRXLMeController getSaveThingOfZero] addObject:thing];
                        
                    }
                    
                    [[LRXBuyThingCell getSelectedArray] removeObject:thing];
                    
                  
                    
                }
                break;
            }
        }
        
//        for (LRXThing *thing in buyThingArray) {
//            NSLog(@"buy:%@:%@",thing,thing.thingCount);
//        }
//        for (LRXThing *thing in selectedThingArray) {
//            NSLog(@"sel:%@:%@",thing,thing.thingCount);
//        }
//        
        if ([self.thingCellDelegate respondsToSelector:@selector(updateThingRemind:)]) {
            
            [self.thingCellDelegate updateThingRemind:[LRXBuyThingCell getSelectedArray]];
        }
        
    }
    if ([thingCount integerValue] <=0) {
        self.jianBtn.hidden = YES;
        self.thingCount.hidden = YES;
    }
    else
    {
        self.jianBtn.hidden = NO;
        self.thingCount.hidden = NO;
        self.thingCount.text = thingCount;
    }
}

- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
    
    if([self.thingCellDelegate respondsToSelector:@selector(updateThingRemind:)])
    {
        
        [self.thingCellDelegate updateThingRemind:[LRXBuyThingCell getSelectedArray]];
        
    }
    for(UIView * pointView in self.selectPointArray) {
        
        [pointView removeFromSuperview];
        
    }
}

@end
